<!--
 * @Descripttion: 出库管理
 * @LastEditors: xzh
 * @LastEditTime: 2023-11-04 22:00:54
-->
<template>
  <el-card>
    <DileTable ref="dileTableRef">
      <template #preColumn>
        <vxe-column
          field="code"
          :title="$t('page.moldSpareArr.outBoundManageArr.code')"
        />
        <vxe-column
          field="name"
          :title="$t('page.moldSpareArr.outBoundManageArr.name')"
        />
        <vxe-column
          field="stockOutDateTime"
          :title="$t('page.moldSpareArr.outBoundManageArr.stockOutDateTime')"
          formatter="formatDate"
        />
        <vxe-column
          field="type"
          :title="$t('page.moldSpareArr.outBoundManageArr.type')"
        >
          <template #default="{ row }">
            <span v-if="!!row.type">{{
              $t(
                `page.moldSpareArr.outBoundManageArr.typeArr.${
                  outBoundTypeEnum[row.type]
                }`
              )
            }}</span>
          </template>
        </vxe-column>
        <vxe-column :title="$t('page.systemManageArr.storeManageArr.name')">
          <template #default="{ row }">
            <span>{{ row.extJson?.warehouseName }}</span>
          </template>
        </vxe-column>
        <vxe-column :title="$t('page.systemManageArr.storeManageArr.manager')">
          <template #default="{ row }">
            <span>{{ row.extJson?.warehouseManagerName }}</span>
          </template>
        </vxe-column>
        <vxe-column
          field="stockOutNumber"
          :title="$t('page.moldSpareArr.outBoundManageArr.stockOutNumber')"
        >
          <template #default="{ row }">
            <span>{{ row.extJson?.stockOutUserNum }}</span>
          </template>
        </vxe-column>
        <vxe-column
          :title="$t('page.moldSpareArr.outBoundManageArr.applicant')"
        >
          <template #default="{ row }">
            <span>{{ row.extJson?.applicantName }}</span>
          </template>
        </vxe-column>
        <vxe-column
          :title="$t('page.moldSpareArr.outBoundManageArr.department')"
        >
          <template #default="{ row }">
            <span>{{ row.extJson?.applicantDepartmentName }}</span>
          </template>
        </vxe-column>
        <vxe-column
          :title="$t('page.moldSpareArr.outBoundManageArr.stockOutUser')"
        >
          <template #default="{ row }">
            <span>{{ row.extJson?.stockOutUserName }}</span>
          </template>
        </vxe-column>
        <vxe-column field="updateUser" :title="$t('table.updateUser')" />
        <vxe-column
          field="updateTime"
          :title="$t('table.updateTime')"
          formatter="formatDateTime"
        />
      </template>
    </DileTable>
    <!-- 出库管理表单 -->
    <OutBoundDrawerFrom ref="outBoundDrawerFromRef" @refresh="initTableData" />
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, provide, onMounted } from "vue";
import {
  btnTypeEnum,
  propsParam_Inf,
} from "@/components/dile-table/index.type";
import type { object_Inf } from "@/utils/typings/index.type";
import { moldSpareModuleToolEnum } from "@/utils/typings/moldManage/moldSpare/index.type";
import { useI18n } from "vue-i18n";
import { outBoundTypeEnum } from "@/utils/typings/equipManage/equipSpare/index.type";
import { ElMessageBox, ElNotification } from "element-plus";
import DileTable from "@/components/dile-table/index.vue";
import OutBoundDrawerFrom from "./outBound-form/fom-drawer/index.vue";
import Dile_APi from "@/api";

/**
 * @Author: xzh
 * @Descripttion:i18n实例
 * @Param:
 */
let { t } = useI18n();

/**
 * @Author: xzh
 * @Descripttion: 出库管理表单实例
 * @Param:
 */
let outBoundDrawerFromRef = ref<any>(null);
/**
 * @Author: xzh
 * @Descripttion: 表格实例
 * @Param:
 */
let dileTableRef = ref<any>(null);

/**
 * @Author: xzh
 * @Descripttion:初始化加载
 * @Param:
 */
onMounted(() => {
  initTableData();
});
/**
 * @Author: xzh
 * @Descripttion:表格加载事件
 * @Param:
 */
let initTableData = async () => {
  try {
    propsParam.loading = true;
    Object.assign(propsParam, {
      tableData: [],
      loading: true,
    });
    let { keyWord, pagination } = dileTableRef.value?.getTableParam(),
      { page, rows } = pagination;
    //获取出库管理列表
    let { record: tableData, total } = await Dile_APi.getMoldOutBoundList({
      keyWord,
      pagination: {
        pageIndex: page,
        pageSize: rows,
      },
    });
    Object.assign(propsParam, {
      tableData,
      tableRecords: total,
    });
  } finally {
    propsParam.loading = false;
  }
};
/**
 * @Author: xzh
 * @Descripttion:工具栏按钮点击事件
 * @Param:
 * @param {*} event
 */
let handleToolBtnClick = (event: object_Inf) => {
  let { code } = event,
    { rowChecked } = dileTableRef.value;
  //![新增]->行勾选判断
  if (!["add"].includes(code) && !rowChecked()) {
    return false;
  }
  switch (code) {
    case "add":
    case "edit":
      //加载出库管理抽屉
      InitOutBoundDrawerFromRefDrawer(code);
      break;
    case "delete":
      //删除
      handleDelete();
      break;
  }
};

/**
 * @Author: xzh
 * @Descripttion:加载出库管理抽屉
 * @Param:
 * @param {*} code
 */
let InitOutBoundDrawerFromRefDrawer = (code: string) => {
  let { clearCheckboxRow, getTableParam } = dileTableRef.value,
    { selectObjArr } = getTableParam(),
    currentRow = selectObjArr?.find(() => true);
  //加载抽屉
  outBoundDrawerFromRef.value.init(code == "edit" ? currentRow.id : 0);
  //清楚行选中
  code == "edit" && clearCheckboxRow([currentRow]);
};
/**
 * @Author: xzh
 * @Descripttion: 删除事件
 * @Param:
 */
let handleDelete = () => {
  ElMessageBox.confirm(
    t("common.message.deleteVerify"),
    t("common.message.confirmInfo"),
    {
      distinguishCancelAndClose: true,
      type: "warning",
      confirmButtonText: t("common.define"),
      cancelButtonText: t("common.cancel"),
    }
  ).then(async () => {
    try {
      propsParam.loading = true;
      let { selectObjArr }: object_Inf = dileTableRef.value.getTableParam();
      //删除选中出库库信息
      await Dile_APi.deleteMoldOutBound(
        selectObjArr.map((x: object_Inf) => x.id)
      );
      //提示成功
      ElNotification({
        message: t("common.message.OperationSuccess"),
        type: "success",
      });
      //刷新表格
      initTableData();
    } finally {
      propsParam.loading = false;
    }
  });
};
/**
 * @Author: xzh
 * @Descripttion: 表格参数
 * @Param:
 */
let propsParam = reactive<propsParam_Inf>({
  moduleToolCode: moldSpareModuleToolEnum.moldOutBoundManage,
  loading: false,
  checkbox: true,
  tableData: [],
  isPagination: true,
  emitEvents: {
    refreshTable: initTableData,
    toolBtnClick: handleToolBtnClick,
  },
});
provide("propsParam", propsParam);
</script>
<style lang="scss" scoped></style>
